package com.njlime.cms.controller.sys;

import com.njlime.cms.entities.Channel;
import com.njlime.cms.entities.TColumn;
import com.njlime.cms.repository.sys.ChannelDao;
import com.njlime.cms.service.sys.ChannelService;
import com.njlime.cms.service.sys.ColumnService;
import com.njlime.core.base.controller.BaseController;
import com.njlime.core.exception.ExceptionState;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.sys.security.util.UserUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @version V1.0
 * @ClassName: 栏目
 * @Description:
 * @Date 2016-4-11 下午11:19:27
 */
@Controller
@RequestMapping(value = "/cms/column")
public class ColumnController extends BaseController {

    Logger logger = Logger.getLogger(ColumnController.class);

    @Resource
    private ChannelService channelService;

    @Resource
    private ChannelDao channelDao;
    @Resource
    private ColumnService columnService;


    @RequestMapping(value = "info", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getColumnInfo(String id, HttpServletRequest request, HttpServletResponse response) {
        try {
            return toJsonSuccessData(columnService.findOne(id));
        } catch (Exception e) {
            logger.error("", e);
            return toJsonErrorData(ExceptionState.PARAM_EXCEPTION, e.getMessage());
        }
    }

    @RequestMapping(value = "add", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String saveColumn(TColumn column,Integer channelType, HttpServletRequest request, HttpServletResponse response) {
        logger.info("column/saveColumn");
        Map<String, Object> jsonList = new HashMap<String, Object>();
        try {
            int unitId = UserUtils.getUser().getRootUnit().getId();
       //     if (UserUtils.getUser().getRootUnit() != null) {
                TColumn info = columnService.getColumnByName(column.getColumn_name(), unitId, channelType);
                if (info != null) {
                    return toJsonErrorData(2, "栏目已存在");
                }
            Map<String, Object> map = channelService.getChannelByShort(unitId, channelType);
            Date date = new Date();
          Channel channel = new Channel();
            channel.setCreate_time(date);
            channel.setChannel_type(Channel.CHANNEL_TYPE_0);
            channel.setChannel_short(channelType);
            channel.setUnit_id(unitId);
            if (map == null) {
                channelDao.save(channel);

            }else{
                channel.setId(Integer.parseInt(map.get("id").toString()));
            }
            column.setChannel_id(channel.getId());
                column.setUnit_id(UserUtils.getUser().getUnit_id());
                column.setIs_del(GlobalValue.NO);
                columnService.save(column);
//            } else {
//                return toJsonErrorData(ExceptionState.MANAGER_EXCEPTION, "");
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return toJsonSuccessData();
    }

    @RequestMapping(value = "edit", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String edit(TColumn column, HttpServletRequest request, HttpServletResponse response) {
        logger.info("Column/updateColumn");
        try {
             int unitId = UserUtils.getUser().getRootUnit().getId();

            if (UserUtils.getUser().getRootUnit() != null) {
                TColumn info = columnService.getColumnByName(column.getColumn_name(), unitId, column.getChannel_id());
                if (info != null && !info.getId().toString().equals(column.getId())) {
                    return toJsonErrorData(2, "栏目已存在");
                }
                columnService.updateColumn(column.getColumn_name(), column.getId(), unitId, column.getChannel_id());
            } else {
                return toJsonErrorData(ExceptionState.MANAGER_EXCEPTION, "");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJsonSuccessData();
    }
//
//

    /**
     * 查询栏目
     *
     * @param request
     * @param channelShort 频道号
     * @return
     */
    @RequestMapping(value = "list", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getColumnList(HttpServletRequest request, int channelShort ) {
        try {
              int unitId = UserUtils.getUser().getRootUnit().getId();
            return toJsonListData(columnService.getColumnList(channelShort,unitId ));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 根据频道查询栏目
     *
     * @param request
     * @param channelId 频道id
     * @return
     */
    @RequestMapping(value = "listByChannelId", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getColumnListByChannelId(HttpServletRequest request, Integer channelId ) {
        try {
            int unitId = UserUtils.getUser().getRootUnit().getId();
            Map<String, Object> map = channelService.getChannelById(unitId, channelId);
            if (map != null) {
                channelId = Integer.parseInt(map.get("id").toString());
            }else{
                channelId = 0;
            }
            return toJsonListData(columnService.getColumnListByChannelId(channelId,unitId ));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 根据频道查询栏目
     *
     * @param request
     * @param channelType 频道序号
     * @return
     */
    @RequestMapping(value = "listByChannelShort", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String listByChannelShort(HttpServletRequest request, Integer channelType ) {
        try {
            int unitId = UserUtils.getUser().getRootUnit().getId();

            Map<String, Object> map = channelService.getChannelByShort(unitId, channelType);
            int channelId = 0;
            if (map != null) {
                channelId = Integer.parseInt(map.get("id").toString());
            }
            return toJsonListData(columnService.getColumnListByChannelId(channelId,unitId ));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 删除栏目
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping(value = "delete", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String delColumn(HttpServletRequest request, String id) {
        logger.info("column/delColumn");
        if (StringUtils.isNotEmpty(id)) {
            if (UserUtils.getUser().getRootUnit() != null) {
                int unitId = UserUtils.getUser().getRootUnit().getId();

                columnService.delete(id, unitId);
            } else {
                return toJsonErrorData(ExceptionState.MANAGER_EXCEPTION, "");
            }
        } else {
        }
        return toJsonSuccessData();
    }
}
